﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace bstest
{
    public partial class FormSectionData : Form
    {
        public FormSectionData()
        {
            InitializeComponent();
        }
		
		//展示当前值
        private void showData()
        {
            int calMode, number;
            double length, elasticity, density;
            string filename = "data.xml";
            dataIO.ImpellerData[] data = new dataIO.ImpellerData[50];
            dataIO.ReadData(filename,out calMode, out data, out number, out length, out elasticity, out density);
            switch (calMode)
            {
                case 1: this.comboBoxCalMode.Text = "振型迭代法"; break;
                case 2: this.comboBoxCalMode.Text = "雷利法"; break;
                case 3: this.comboBoxCalMode.Text = "传递矩阵法"; break;
            }
            this.textBoxLength.Text = length.ToString();
            this.textBoxElasticity.Text = elasticity.ToString();
            this.textBoxDensity.Text = density.ToString();
            this.textBoxNumber.Text = number.ToString();
            int i;
            showDataTextBox(number);
            string namel, namea, namej;
            for (i=0;i<=number;i++)
            {
                foreach (Control c in this.paneldata.Controls)
                {
                    namel = "data" + i.ToString() + 'l';
                    namea = "data" + i.ToString() + 'a';
                    namej = "data" + i.ToString() + 'j';
                    if (c.Name == namel) c.Text = data[i].l.ToString();
                    if (c.Name == namea) c.Text = data[i].a.ToString();
                    if (c.Name == namej) c.Text = data[i].j.ToString();
                }
            }
        }

		//将输入界面中的输入存入文件
        private void WriteData()
        {
            int number = Convert.ToInt16(this.textBoxNumber.Text);
            double length = Convert.ToDouble(this.textBoxLength.Text);
            double elasticity = Convert.ToDouble(this.textBoxElasticity.Text);
            double density = Convert.ToDouble(this.textBoxDensity.Text);
            string cal = " ";
            string filename = "data.xml";
            if (comboBoxCalMode.SelectedItem!=null) cal = comboBoxCalMode.SelectedItem.ToString();
            int calMode=0,i;
            switch (cal)
            {
                case "振型迭代法": calMode = 1; break;
                case "雷利法": calMode = 2;break;
                case "传递矩阵法": calMode = 3; break;
            }
            dataIO.ImpellerData[] data = new dataIO.ImpellerData[number + 1];
            string namel, namea, namej;
            for (i = 0; i <= number; i++)
            {
                foreach (Control c in this.paneldata.Controls)
                {
                    namel = "data" + i.ToString() + 'l';
                    namea = "data" + i.ToString() + 'a';
                    namej = "data" + i.ToString() + 'j';
                    if (c.Name == namel) data[i].l = Convert.ToDouble(c.Text);
                    if (c.Name == namea) data[i].a = Convert.ToDouble(c.Text);
                    if (c.Name == namej) data[i].j = Convert.ToDouble(c.Text);
                }
            }
            dataIO.WriteData(filename,calMode, data, number, length, elasticity, density);
        }

        private void buttonSectionClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

		//重置数据
        private void buttonInitializate_Click(object sender, EventArgs e)
        {
            dataIO.InitializationData();
            showData();
        }

        private void FormSectionData_Load(object sender, EventArgs e)
        {
            showData();//打开界面时展示数据
        }

        private void buttonSectionInput_Click(object sender, EventArgs e)
        {
            WriteData();//点击确定后写入数据
            this.Close();
        }

		//根据输入的段数，动态显示出相应的输入textbox
        private void showDataTextBox(int n)
        {
            paneldata.Controls.Clear();
            int x = -250, y = 40,i;
            Label l1 = new Label();Label l2 = new Label();Label l3 = new Label();
            l1.Name = "LabelLength";l2.Name = "Labela";l3.Name = "Labelj";
            l1.Text = "位置";l2.Text = "截面积";l3.Text = "转矩";
            l1.Location = new Point(30,20);l2.Location = new Point(110, 20);l3.Location = new Point(190, 20);
            l1.Size= new System.Drawing.Size(60, 20); l2.Size = new System.Drawing.Size(60, 20); l3.Size = new System.Drawing.Size(60, 20);
            this.paneldata.Controls.Add(l1);this.paneldata.Controls.Add(l2);this.paneldata.Controls.Add(l3);
            l1.Show(); l2.Show(); l3.Show();
            for (i = 0; i <= n; i++)
            {
                if (i % 12 == 0)
                {
                    x += 280; y = 40;
                }
                TextBox tbl = new TextBox();
                tbl.Name = "data" + i.ToString() + 'l';
                tbl.Location = new Point(x, y);
                tbl.Size = new System.Drawing.Size(60, 20);
                this.paneldata.Controls.Add(tbl);
                tbl.Show();
                TextBox tba = new TextBox();
                tba.Name = "data" + i.ToString() + 'a';
                tba.Location = new Point(x + 80, y);
                tba.Size = new System.Drawing.Size(60, 20);
                this.paneldata.Controls.Add(tba);
                tba.Show();
                TextBox tbj = new TextBox();
                tbj.Name = "data" + i.ToString() + 'j';
                tbj.Location = new Point(x + 160, y);
                tbj.Size = new System.Drawing.Size(60, 20);
                this.paneldata.Controls.Add(tbj);
                tbj.Show();
                y += 25;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int number = Convert.ToInt16(this.textBoxNumber.Text);
            showDataTextBox(number);
        }
    }
}
